package com.nyf.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.nyf.entity.Document;
import com.nyf.util.DBHelp;
import com.nyf.util.RowMapper;

public class DocumentDao {

private DBHelp<Document> db=new DBHelp<Document>();
	
	
	public void save(Document doc) {
		String sql = "insert into t_document(name,filename,time,fileid,kind) values(?,?,?,?,?)";
		db.executeSQL(sql, doc.getName(),doc.getFilename(),doc.getTime(),doc.getFileid(),doc.getKind());
	}
	
	public List<Document> findAll() {
		String sql = "select id,name,filename,time,fileid,kind from t_document";
		return db.executeQueryForList(sql, new DocumentRowMapper());
	}
	public List<Document> findByfileId(String fileId) {
		String sql = "SELECT * FROM t_document WHERE ?";
		return db.executeQueryForList(sql, new DocumentRowMapper(), fileId);
	}
	
	private class DocumentRowMapper implements RowMapper<Document> {

		public Document mapRow(ResultSet rs) throws SQLException {
			Document f = new Document();
			f.setFilename(rs.getString("filename"));
			f.setId(rs.getInt("id"));
			f.setName(rs.getString("name"));
			f.setTime(rs.getString("time"));
			f.setFileid(rs.getString("fileid"));
			f.setKind(rs.getString("kind"));
			return f;
		}
		
	}
}
